package Iostart;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class IoMain
{
	public static void main(String[] args)
	{
		File lest01 = new File("D:/a");
		File lest02 = new File("D:/b");
		int count = 0;
//		tree(lest01, count);
		System.out.println("===============");
		digui(lest01, lest02, count);
//		readFile();
	}

	private static void readFile()
	{
		FileInputStream re = null;
		try
		{
			File aa = new File("D:/a/a.txt");
			re = new FileInputStream(aa);
			byte[] b = new byte[3];
			int len = 0;
			while ((len = re.read(b)) != -1)
			{
				System.out.println(Arrays.toString(b));
			}

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (re != null)
				{
					re.close();
					re = null;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}

	}

	static void digui(File aa, File bb, int count)
	{
		String str = "";
		StringBuilder rest = new StringBuilder("|-->");
		for (int i = 0; i < count; i++)
		{
			rest.append("|-->");
		}
		File[] lestOne = aa.listFiles();
		for (int i = 0; i < lestOne.length; i++)
		{
			File childFile = lestOne[i];
			System.out.println(rest.toString() + childFile.getName());
			if (childFile.isDirectory())
			{
				str = bb.getPath() + "\\" + lestOne[i].getName();
				File ff = new File(str);
				ff.mkdir();
				digui(childFile, ff, count + 1);
			}else
			{
				FileInputStream res = null;
				BufferedInputStream  re = null;
				FileOutputStream fos = null;
				BufferedOutputStream bos = null;
				try
				{
					res = new FileInputStream(childFile);
					re = new BufferedInputStream(res);
					str = bb.getPath() + "\\" + childFile.getName();
					File f = new File(str);
					f.createNewFile();
					fos = new FileOutputStream(f);
					bos = new BufferedOutputStream(fos);
					
					byte[] b = new byte[3];
					int len = 0;
					while ((len = re.read(b)) != -1)
					{
//						System.out.println(Arrays.toString(b));
						bos.write(b, 0, len);
					}
					bos.flush();
				} catch (FileNotFoundException e)
				{
					e.printStackTrace();
				} catch (IOException e)
				{
					e.printStackTrace();
				} catch (Exception e)
				{
					e.printStackTrace();
				} finally
				{
					try
					{
						if (re != null)
						{
							re.close();
							re = null;
						}
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				}
			}
		}
	}

	private static void tree(File tarFile, int level)
	{
		StringBuffer sb = new StringBuffer();
		/* 增加前置字符串 */
		for (int i = 0; i < level; i++)
		{
			sb.append("|-->");
		}
		/* 一开始就打印 */
		System.out.println(sb.toString() + tarFile.getName());

		/* 列出目录下面的文件和文件夹 */
		if (tarFile.isDirectory())
		{
			File[] childFiles = tarFile.listFiles();
			for (int i = 0; i < childFiles.length; i++)
			{
				/* 子文件夹 */
				File childFile = childFiles[i];
				// System.out.println(sb.toString() + childFile.getName());
				/* 只能是文件夹的时候才能列 */
				/* 这个是目录 */
				tree(childFile, level + 1);
			}
		}
	}

}
